package cn.fl.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.flow.facade.flow.constant.FlowConstant;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.web.core.framework.constant.SysOrgConstant;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fribaseinfo.vo.BankAccInfoVO;
import cn.fl.system.condition.SysBkAccInfoQueryCondition;
import cn.fl.system.constant.SysBkInfoConstant;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.dto.SysBkAccInfoDTO;
import cn.fl.system.service.ISysBkAccInfoService;
import cn.fl.system.service.ISysBkInfoService;
import cn.fl.system.transform.SysBkInfoVOTransform;
import cn.fl.system.vo.SysBkAccInfoVO;
import cn.fl.system.vo.SysBkAccOrgRVO;
import cn.fl.system.vo.SysBkInfoVO;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.flow.facade.flow.dto.FlowStartedDTO;
import fl.ref.fnccappayeeinfobig.constant.FncCapPayeeInfoBigConstant;
import fl.ref.fribaseinfo.constant.FriBaseInfoConstant;
import net.sf.json.JSONArray;

/**
 * @Description: 银行账号信息表 控制层
 */
@Controller
@RequestMapping("/system/sysBkAccInfo")
public class SysBkAccInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysBkAccInfoController.class);

	/**
	 * SysBkAccInfo 服务层BEAN
	 */
	@Autowired
	private ISysBkAccInfoService sysBkAccInfoService;
	/**
	 * SysBkInfo 服务层BEAN
	 */
	@Autowired
	private ISysBkInfoService sysBkInfoService;
	/**
	 * 组织架构服务
	 */
	@Autowired
	private ISysOrgApiService sysOrgApiService;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		String bkId = request.getParameter("bkId");
		BaseLogUtils.info(logger, "toListPage", "加载银行账号列表页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysBkAccInfo/sysSysBkAccInfoList");
		view.addObject("bkId", bkId);
		//付款帐号主体
		List<KeyValueVO> payerMainCdList = this.sysBkAccInfoService.getKvList(SysBkInfoConstant.SYS_BK_ACC_PAYER_MAIN_CD_STRING);
		view.addObject("payerMainCdList", payerMainCdList);
					
		//组织架构
		List<SysOrgDTO> sysOrgList = this.sysOrgApiService.searchSysOrgByOrgPid(SysOrgConstant.SYS_ORG_CD_PID_INIT, CurrentThreadContext.getCurrentSysCd());
		//List<KeyValueVO> sysOrgList = this.sysBkAccInfoService.searchSysOrgByOrgLvl(SysOrgConstant.SYS_ORG_CD_PID_INIT);
		view.addObject("sysOrgList", sysOrgList);
		return view;
	}

	/**
	 * 到新增页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toModifyPage", "加载银行账号添加页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysBkAccInfo/sysSysBkAccInfoAdd");
		
		//开户行
		List<SysBkInfoVO> openBkList = this.sysBkAccInfoService.searchOpenBkInfo();
		view.addObject("openBkList", openBkList);
		//账号类型
		List<KeyValueVO> bkAccTypCdList = this.sysBkAccInfoService.searchBkAccTypCdList();
		view.addObject("bkAccTypCdList", bkAccTypCdList);
		//付款帐号主体
		List<KeyValueVO> payerMainCdList = this.sysBkAccInfoService.getKvList(SysBkInfoConstant.SYS_BK_ACC_PAYER_MAIN_CD_STRING);
		view.addObject("payerMainCdList", payerMainCdList);
				
		//组织架构
		List<SysOrgDTO> sysOrgDTOList = this.sysOrgApiService.searchSysOrgByOrgPid(SysOrgConstant.SYS_ORG_CD_PID_INIT, CurrentThreadContext.getCurrentSysCd());
		//List<KeyValueVO> sysOrgList = this.sysBkAccInfoService.searchSysOrgByOrgLvl(SysOrgConstant.SYS_ORG_CD_PID_INIT);
		view.addObject("sysOrgList", sysOrgDTOList);
		return view;
	}
	@RequestMapping(value = "/getOpenBkData.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getPrdTyeJson(HttpServletRequest request, HttpServletResponse response) {
		
		try {
			List<SysBkInfoVO> openBkList = this.sysBkAccInfoService.searchOpenBkInfo();
			JSONArray openBkListJSON = JSONArray.fromObject( openBkList.toArray());
			BaseLogUtils.info(logger, "getPrdTyeJson", "获取开户行树数据成功.DATA:"+JSONObject.toJSONString(openBkList),CurrentThreadContext.getCurrentUserName());
			return openBkListJSON;
		} catch (Exception e) {
			BaseLogUtils.error(logger, "getPrdTyeJson", "获取开户行树数据失败.ERROR:"+e.getMessage(),CurrentThreadContext.getCurrentUserName());
		}
		return null;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initBkAccList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String bkId = request.getParameter("bkId");
		
		String bkNm = request.getParameter("bkNm");
		String accNm = request.getParameter("accNm1");
		
		String sortName = request.getParameter("sortname");
		String sortOrder = request.getParameter("sortorder");

        String accNo = request.getParameter("accNo");
        String isValid = request.getParameter("isValid");
        String payerMainCd = request.getParameter("payerMainCd");
        
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysBkAccInfoQueryCondition qc = new SysBkAccInfoQueryCondition();
		if (StringUtils.isNotEmpty(bkNm)) {
			qc.setBkNm(bkNm);
		}
		if (StringUtils.isNotEmpty(accNm)) {
			qc.setAccNm(accNm);
		}

		if(StringUtils.isNotEmpty(sortName) && StringUtils.isNotEmpty(sortOrder)){
			qc.setSortName(sortName);
			qc.setSortOrder(sortOrder);
		}

        if (StringUtils.isNotEmpty(accNo)) {
            qc.setAccNo(accNo);
        }
        if (StringUtils.isNotEmpty(isValid)) {
            qc.setIsValid(Integer.valueOf(isValid));
        }

        if (StringUtils.isNotEmpty(payerMainCd)) {
            qc.setPayerMainCd(payerMainCd);
        }
        
		List<Long> bkIds = new ArrayList<Long>();
		if (StringUtils.isNotEmpty(bkId)) {
			bkIds.add(Long.valueOf(bkId));
		}
		qc.setBkIds(bkIds);
		
		//当前登录人所属架构代码去前四位设为查询条件
		qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd().substring(0, 4));
		
		page.setCondition(qc);

		BaseLogUtils.info(logger, "initBkAccList", "初始化银行账号列表页面数据.DATA:"+JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		//TODO SOMETHING
		Pager pager = this.sysBkAccInfoService.searchListPage(page);
		List<SysBkAccInfoVO> list = (List<SysBkAccInfoVO>) pager.getResultList();
		//查找银行
		for (int i = 0; i < list.size(); i++) {
			SysBkAccInfoVO dto=list.get(i);
			if (dto.getBkId()!=null) {
				dto.setSysBkInfoVO(this.sysBkInfoService.selectById(dto.getBkId()));
			}
		}
		ListPageVO<SysBkAccInfoVO> listPage = new ListPageVO<SysBkAccInfoVO>(list, pager.getTotalCount());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeBkAccById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String ids = request.getParameter("ids");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(ids)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "removeBkAccById", "删除银行账号数据,主键参数为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		List<Long> idsList = new ArrayList<Long>();
		JSONArray idsArray = JSONArray.fromObject(ids);
		for (Object object : idsArray) {
			idsList.add(Long.valueOf(object.toString()));
		}
		
		try {
			boolean success = this.sysBkAccInfoService.removeByIds(idsList);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				BaseLogUtils.info(logger, "removeBkAccById", "删除银行账号数据失败,ID:"+idsList.toString(), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "removeBkAccById", "删除银行账号数据成功,ID:"+idsList.toString(), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeBkAccById", "删除银行账号数据失败,ID:"+idsList.toString()+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeBkAccById", "删除银行账号数据失败,系统错误.ID:"+idsList.toString()+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getBkAccById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "getBkAccById", "获取银行账号数据,主键参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			SysBkAccInfoVO vo = this.sysBkAccInfoService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getBkAccById", "获取银行账号数据不存在,ID:"+id, CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getBkAccById", "获取银行账号数据成功,ID:"+id, CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getBkAccById", "获取银行账号数据失败,ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getBkAccById", "获取银行账号数据失败,系统错误.ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addBkAcc(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		SysBkAccInfoVO entity = new SysBkAccInfoVO();
		try {
			entity = this.getRequestParamForSysBkAccAdd(request);
			Long id = this.sysBkAccInfoService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "addBkAcc", "新增银行账号数据成功,ID:"+id+".DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "addBkAcc", "新增银行账号数据失败.DATA:"+JSONObject.toJSONString(entity)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addBkAcc", "新增银行账号数据失败,系统错误.DATA:"+JSONObject.toJSONString(entity)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
	
	/**
	 * 导出数据
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	@RequestMapping(value = "/exportBkAcc.do")
	@ResponseBody
	public Object exportBkAcc(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String ids = request.getParameter("ids");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_BK_EXPORT_SUCC);
		
		if(StringUtils.isEmpty(ids)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "exportBkAcc", "导出银行账号数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		List<Long> idsList = new ArrayList<Long>();
		JSONArray idsArray = JSONArray.fromObject(ids);
		for (Object object : idsArray) {
			idsList.add(Long.valueOf(object.toString()));
		}
		
		try {
			String path = this.sysBkAccInfoService.exportBkAcc(idsList, request, response);
			if(StringUtils.isNotEmpty(path)){
				result.setSuccess(BaseConstant.IS_YESNO_YES);
				result.setInfo(SysLogConstant.LOG_BK_EXPORT_SUCC);
				result.setData(path);
				BaseLogUtils.info(logger, "exportBkAcc", "导出银行账号数据成功,IDS:"+idsList.toString(), CurrentThreadContext.getCurrentUserName());
			}
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "exportBkAcc", "导出银行账号数据失败,IDS:"+idsList.toString()+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "exportBkAcc", "导出银行账号数据失败,系统错误.IDS:"+idsList.toString()+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		
		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyBkAcc(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,SysLogConstant.LOG_CMM_UPDATE_SUCC);
		SysBkAccInfoVO entity = new SysBkAccInfoVO();
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modifyBkAcc", "修改银行账号信息,主键参数为空！.ID:"+id, CurrentThreadContext.getCurrentUserName());
			return result;
		}
		try {
			entity = this.getRequestParamForSysBkAccModify(request);
			entity.setId(Long.valueOf(id));
			boolean success = this.sysBkAccInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
				BaseLogUtils.info(logger, "modifyBkAcc", "修改银行账号数据失败.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modifyBkAcc", "修改银行账号数据成功.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modifyBkAcc", "修改银行账号数据失败.DATA:"+JSONObject.toJSONString(entity)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modifyBkAcc", "修改银行账号数据失败,系统错误.DATA:"+JSONObject.toJSONString(entity)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 启用账号
	 */
	@RequestMapping(value = "/enable.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object enableBkAcc(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,SysLogConstant.LOG_CMM_ENABLE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "enableBkAcc", "启用银行账号,主键参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}

		try {
			
			boolean success = this.sysBkAccInfoService.enAbleSysBkAccInfo(Long.valueOf(id));
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_ENABLE_FAIL);
				BaseLogUtils.info(logger, "enableBkAcc", "启用银行账号失败,ID:"+id, CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "enableBkAcc", "启用银行账号成功,ID:"+id, CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "enableBkAcc", "启用银行账号失败,ID:"+id+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "enableBkAcc", "启用银行账号失败,系统错误.ID:"+id+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 禁用账号
	 */
	@RequestMapping(value = "/disable.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object disableBkAcc(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,SysLogConstant.LOG_CMM_DISABLE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "disableBkAcc", "禁用银行账号,主键参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			
			boolean success = this.sysBkAccInfoService.disAbleSysBkAccInfo(Long.valueOf(id));
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DISABLE_FAIL);
				BaseLogUtils.info(logger, "disableBkAcc", "禁用银行账号失败,ID:"+id, CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "disableBkAcc", "禁用银行账号成功,ID:"+id, CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "disableBkAcc", "禁用银行账号失败,ID:"+id+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "disableBkAcc", "禁用银行账号失败,系统错误.ID:"+id+".ERROR"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	//********************私有方法********************//
		/**
		 * 获取银行账号参数信息
		 * @param request
		 * @return
		 * @throws BaseException
		 */
		private SysBkAccInfoVO getRequestParamForSysBkAcc(HttpServletRequest request)throws BaseException{
			String accTypCd = request.getParameter("accTypCd");//账号类型
			String bkId = request.getParameter("bkId");//开户行
			String accNm = request.getParameter("accNm");//账户名称
			String accNo = request.getParameter("accNo");//账号
			String accRem = request.getParameter("accRem");//备注
			
			/*String orgIds = request.getParameter("orgIds");
			List<Long> orgIdsList = new ArrayList<Long>();
			JSONArray idsArray = JSONArray.fromObject(orgIds);
			for (Object object : idsArray) {
				orgIdsList.add(Long.valueOf(object.toString()));
			}
			if(CollectionUtils.isEmpty(orgIdsList)){
				//不选默认维护所有关系 调接口获取所有平台id  
				List<KeyValueVO> kvList = this.sysBkAccInfoService.searchSysOrgByOrgLvl(SysOrgConstant.SYS_ORG_CD_PID_INIT);
				if(CollectionUtils.isNotEmpty(kvList)){
					for (KeyValueVO kv : kvList) {
						orgIdsList.add(Long.valueOf(kv.getKey()));
					}
				}
			}*/
			List<SysBkAccOrgRVO> sysBkAccOrgRVOList = new ArrayList<SysBkAccOrgRVO>();
			String orgJSON = request.getParameter("orgJSON");
			if(StringUtils.isNotEmpty(orgJSON)){
				sysBkAccOrgRVOList = JSON.parseArray(orgJSON, SysBkAccOrgRVO.class);
			}
			if(CollectionUtils.isEmpty(sysBkAccOrgRVOList)){
				//不选默认维护所有平台关系
				List<SysOrgDTO> sysOrgList = this.sysOrgApiService.searchSysOrgByOrgPid(SysOrgConstant.SYS_ORG_CD_PID_INIT, CurrentThreadContext.getCurrentSysCd());
				if(CollectionUtils.isNotEmpty(sysOrgList)){
					for (SysOrgDTO sysOrgDTO : sysOrgList) {
						SysBkAccOrgRVO sysBkAccOrgRVO = new SysBkAccOrgRVO();
						sysBkAccOrgRVO.setOrgId(sysOrgDTO.getId());
						sysBkAccOrgRVO.setOrgCd(sysOrgDTO.getOrgCd());
						sysBkAccOrgRVOList.add(sysBkAccOrgRVO);
					}
				}
			}
			
			
			//验证必填项及长度
			if(StringUtils.isBlank(accTypCd)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "请选择账号类型。");
			}
			
			if(StringUtils.isBlank(bkId) ){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "请选择开户行");
			}
			
			if(StringUtils.isBlank(String.valueOf(accNm)) ){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "账号名称不能为空");
			}
			
			/*if(StringUtils.isBlank(String.valueOf(accNo))||StringUtils.isNumeric(accNo) ){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "账号不能为空,并且需填写正确的账号格式");
			}*/
			
			
			SysBkAccInfoVO sysBkAccInfoVO = new SysBkAccInfoVO();
			sysBkAccInfoVO.setAccTypCd(accTypCd.trim());
			sysBkAccInfoVO.setBkId(Long.valueOf(bkId.trim()));
			sysBkAccInfoVO.setAccNm(accNm.trim());
			sysBkAccInfoVO.setAccNo(accNo.trim());
			sysBkAccInfoVO.setAccRem(accRem.trim());
			sysBkAccInfoVO.setSysBkAccOrgRVOList(sysBkAccOrgRVOList);
			sysBkAccInfoVO.setPayerMainCd(request.getParameter("payerMainCd"));
			return sysBkAccInfoVO;
		}
		
		/**
		 * 获取银行账号请求参数(新增)
		 * @param request 请求
		 * @return 
		 */
		private SysBkAccInfoVO getRequestParamForSysBkAccAdd(HttpServletRequest request)throws BaseException{
			SysBkAccInfoVO sysBkAccInfoVO = this.getRequestParamForSysBkAcc(request);
			sysBkAccInfoVO.setCrtTm(new Date());
			sysBkAccInfoVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			return sysBkAccInfoVO;
		}
		
		/**
		 * 获取银行账号请求参数(修改)
		 * @param request 请求
		 * @return 
		 */
		private SysBkAccInfoVO getRequestParamForSysBkAccModify(HttpServletRequest request)throws BaseException{
			SysBkAccInfoVO sysBkAccInfoVO = this.getRequestParamForSysBkAcc(request);
			sysBkAccInfoVO.setMdfTm(new Date());
			sysBkAccInfoVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			return sysBkAccInfoVO;
		}


    /**
     * 到选择列表页面
     */
    @RequestMapping(value = "/toChooseListPage.do", method = {RequestMethod.GET})
    public ModelAndView toChooseListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toChooseListPage", "加载银行账户选择列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("system/sysBkAccInfo/sysBkAccInfoChoose");
        mav.addObject("flag", request.getParameter("flag"));
        return mav;
    }
    /**
     * 到选择列表页面2
     */
    @RequestMapping(value = "/toChooseListPage2.do", method = {RequestMethod.GET})
    public ModelAndView toChooseListPage2(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toChooseListPage", "加载银行账户选择列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("system/sysBkAccInfo/sysBkAccInfoChoose2");
        //付款公司主体
        mav.addObject("payerMainList", 
				sysDictionaryApiService.getKvList(FncCapPayeeInfoBigConstant.FNC_PAYER_MAIN,
						CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd()));
        mav.addObject("flag", request.getParameter("flag"));
        return mav;
    }
    
    
    /**
	 * 我司账户详情页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/myAccNoDetail.do")
    public ModelAndView myAccNoDetail(HttpServletRequest request) {
		
		ModelAndView modelAndView = new ModelAndView("system/sysBkAccInfo/myAccNoDetail");
		
		String id = request.getParameter("id");//我司账户id
		if(StringUtils.isNotBlank(id)) {
			SysBkAccInfoVO sysBkAccInfoVO = this.sysBkAccInfoService.selectById(Long.valueOf(id));
			modelAndView.addObject("sysBkAccInfoVO",sysBkAccInfoVO);
		}

		return modelAndView;
    }
	/**
	 * 发起狮桥自有账户审核流程
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addFlowStartOwnAccount.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addFlowStartOwnAccount(HttpServletRequest request, HttpServletResponse response) {

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if (StringUtils.isEmpty(id)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "addFlowStartOwnAccount", SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY, CurrentThreadContext.getCurrentUserName());
			return result;
		}
		FlowStartParamsDTO flowStartParamsDTO = new FlowStartParamsDTO();
		try {
			// 流程定义Key和流程定义ID 如果是前端选择的则直接指定,如果不是则系统自动按照配置选择唯一一条记录,如果找不到会提示
			flowStartParamsDTO.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN);
			flowStartParamsDTO.setWordsInTheTable(FriBaseInfoConstant.REF_FLOW_S_H_FRI);
			//flowStartParamsDTO.setProcDefKey(procDefKey);
			//flowStartParamsDTO.setProcDefId(procDefId);
			//flowStartParamsDTO.setProcNo(procNo);
			flowStartParamsDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			flowStartParamsDTO.setBusinessKey(id);
			flowStartParamsDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			flowStartParamsDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
			flowStartParamsDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
			flowStartParamsDTO.setInitiator(String.valueOf(CurrentThreadContext.getCurrentUserId()));
			flowStartParamsDTO.setInitiatorNm(CurrentThreadContext.getCurrentUserName());
			flowStartParamsDTO.setProcPriority(FlowConstant.FLOW_PRIORITY_EMERGENCY);// 优先级,可以在界面定义
			//flowStartParamsDTO.setBusinessType(FriBaseInfoConstant.FLOW_BUSINESS_TYP_FUND_RAS_INS_BASE_INFO);// 确认用不用
			flowStartParamsDTO.setModelTyp(FriBaseInfoConstant.BK_ACC_FLOW_MODEL_TYP);// 需要和流程配置中的模块类型匹配
			flowStartParamsDTO.setSearchDesc(CurrentThreadContext.getCurrentOrgDispNm() + "、狮桥自有账户审核大字段参数未定!");
			DataResultDTO dataResultDTO = this.sysBkInfoService.addFlowStartOwnAccount(flowStartParamsDTO);
			if (!dataResultDTO.getCode().equals(BaseExceptionConstant.BASE_EXCEPTION_CODE_200)) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				//result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
			}
			if (null != dataResultDTO && null != dataResultDTO.getData()) {
				FlowStartedDTO flowStartedDTO = (FlowStartedDTO) dataResultDTO.getData();
				/*
				if (StringUtils.isNotBlank(flowStartedDTO.getNextOperUsrNames())) {
					result.setInfo("下一个节点操作人:" + flowStartedDTO.getNextOperUsrNames());
				} else {
					result.setInfo("流程发起成功");
				}*/
				result.setData(dataResultDTO.getData());
				result.setInfo("流程发起成功");
			}
		} catch (BaseException e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(e.getMessage());
			BaseLogUtils.info(logger, "addFlowStartOwnAccount",
					"错误[BaseException],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" + e.getMessage(),
					CurrentThreadContext.getCurrentUserName());
			e.getStackTrace();
		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addFlowStartOwnAccount",
					"错误[Exception],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" + e.getMessage(),
					CurrentThreadContext.getCurrentUserName());
			e.getStackTrace();
		}
		return result;
	}
	/**
	 * 狮桥自有账户审核流程流程附加信息页面 修改页面
	 */
	@RequestMapping(value = "/toBkAccInforModifyPage.do", method = { RequestMethod.GET})
	public ModelAndView toBkAccInforModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");//自有账户id
		BaseLogUtils.info(logger, "toBkAccInforModifyPage", "加载银行账号列表页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysBkAccInfo/BkAccInforModifyView");//
		view.addObject("id", id);
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "toBkAccInforModifyPage", SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY, CurrentThreadContext.getCurrentUserName());
			return view;
		}
		//根据账户id获取object
		SysBkAccInfoVO sysBkAccInfoVO=sysBkAccInfoService.selectById(Long.parseLong(id));
		view.addObject("sysBkAccInfoVO",sysBkAccInfoVO);
		//组织架构
		List<SysOrgDTO> sysOrgList = this.sysOrgApiService.searchSysOrgByOrgPid(SysOrgConstant.SYS_ORG_CD_PID_INIT, CurrentThreadContext.getCurrentSysCd());
		view.addObject("sysOrgList", sysOrgList);
		//开户行
		List<SysBkInfoVO> openBkList = this.sysBkAccInfoService.searchOpenBkInfo();
		view.addObject("openBkList", openBkList);
		//账号类型
		List<KeyValueVO> bkAccTypCdList = this.sysBkAccInfoService.searchBkAccTypCdList();
		view.addObject("bkAccTypCdList", bkAccTypCdList);
		return view;
	}
	/**
	 * 狮桥自有账户审核流程流程附加信息页面 查看页面
	 */
	@RequestMapping(value = "/toBkAccInforDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toBkAccInforDetailPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		BaseLogUtils.info(logger, "toBkAccInforDetailPage", "加载银行账号列表页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysBkAccInfo/BkAccInforDetailView");
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "toBkAccInforDetailPage", SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY, CurrentThreadContext.getCurrentUserName());
			return view;
		}
		//根据账户id获取object
		SysBkAccInfoVO sysBkAccInfoVO=sysBkAccInfoService.selectById(Long.parseLong(id));
		view.addObject("sysBkAccInfoVO",sysBkAccInfoVO);
		//组织架构
		List<SysOrgDTO> sysOrgList = this.sysOrgApiService.searchSysOrgByOrgPid(SysOrgConstant.SYS_ORG_CD_PID_INIT, CurrentThreadContext.getCurrentSysCd());
		view.addObject("sysOrgList", sysOrgList);
		//开户行
		List<SysBkInfoVO> openBkList = this.sysBkAccInfoService.searchOpenBkInfo();
		view.addObject("openBkList", openBkList);
		//账号类型
		List<KeyValueVO> bkAccTypCdList = this.sysBkAccInfoService.searchBkAccTypCdList();
		view.addObject("bkAccTypCdList", bkAccTypCdList);
		return view;
	}
	/**
	 * 到流程列表页面
	 */
	@RequestMapping(value = "/toViewFlow.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toViewFlow(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("system/sysBkAccInfo/bkAccInfoFlowList");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toViewFlow", "跳转到页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
}

